<?xml version="1.0" encoding="UTF-8" ?>

<!-- 
One XLST that use HTML + CSS to display a table with all the teams showing 
for each team, the club name, the current score, the number of games, the 
number of wins, number of ties, and number of loses, the number of goals 
scored and the number of goals conceded, and the average of goals scored 
and conceded. The XSLT should receive two parameters, one to indicate the 
column to be used to sort and the other to indicate sort direction. 
 -->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes"/>
	
	<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="xslt/teams_page.xslt"</xsl:processing-instruction>
      <teams>

      <xsl:for-each select="/league/teams/team">
	      <xsl:variable name="currentTeam" select="@id" />
	      
	      
	      <xsl:variable name="homeGamesPlayed" select="count(/league/fixtures/game/home_team[@teamId=$currentTeam])" />
	      <xsl:variable name="awayGamesPlayed" select="count(/league/fixtures/game/visitor_team[@teamId=$currentTeam])" />
	      <xsl:variable name="gamesPlayed" select="$homeGamesPlayed + $awayGamesPlayed" />
	      
		<xsl:variable name="homeWins" select="count( /league/fixtures/game[./home_team[@teamId=$currentTeam] and number(./score/home) > number(./score/visitor)  ] )" />
		<xsl:variable name="awayWins" select="count( /league/fixtures/game[./visitor_team[@teamId=$currentTeam] and number(./score/visitor) > number(./score/home)  ] )" />
		<xsl:variable name="wins" select="$homeWins + $awayWins"/>
		
		
	<xsl:variable name="homeDraws" select="count( /league/fixtures/game[./home_team[@teamId=$currentTeam] and number(./score/home) = number(./score/visitor)  ] )" />
	<xsl:variable name="awayDraws" select="count( /league/fixtures/game[./visitor_team[@teamId=$currentTeam] and number(./score/home) = number(./score/visitor)  ] )" />
	<xsl:variable name="draws" select="$homeDraws + $awayDraws"/>
	
	
	<xsl:variable name="homeLoses" select="count( /league/fixtures/game[./home_team[@teamId=$currentTeam] and number(./score/visitor) > number(./score/home)  ] )" />
	<xsl:variable name="awayLoses" select="count( /league/fixtures/game[./visitor_team[@teamId=$currentTeam] and number(./score/home) > number(./score/visitor)  ] )" />
	
	
	      <xsl:variable name="awayGoalsScored"  select="sum(/league/fixtures/game[./visitor_team[@teamId=$currentTeam]]/score/visitor)"/>
	      <xsl:variable name="homeGoalsScored" select="sum(/league/fixtures/game[./home_team[@teamId=$currentTeam]]/score/home)" />
	      
	      <xsl:variable name="homeGoalsConceded" select="sum(/league/fixtures/game[./home_team[@teamId=$currentTeam]]/score/visitor)" />
	      <xsl:variable name="awayGoalsConceded"  select="sum(/league/fixtures/game[./visitor_team[@teamId=$currentTeam]]/score/home)"/>
	      <team>
	      	<xsl:attribute name="id" select="@id" />
	      	<name><xsl:value-of select="teamName"/></name>
	      	<points><xsl:value-of select="$wins * 3 + $draws"/></points>
	      	<gamesPlayed>
	      		<xsl:attribute name="home">
			    <xsl:value-of select="$homeGamesPlayed" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayGamesPlayed" />
			  </xsl:attribute>
	      		<xsl:value-of select="$gamesPlayed"/>
	      	</gamesPlayed>
	      	<wins>
	      		<xsl:attribute name="home">
			    <xsl:value-of select="$homeWins" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayWins" />
			  </xsl:attribute>
	      	
	      		<xsl:value-of select="$wins"/>
	      	</wins>
	      	<draws>
		      	<xsl:attribute name="home">
			    <xsl:value-of select="$homeDraws" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayDraws" />
			  </xsl:attribute>
	      		<xsl:value-of select="$draws"/>
	      	</draws>
	      	<loses>
		      	<xsl:attribute name="home">
			    <xsl:value-of select="$homeLoses" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayLoses" />
			  </xsl:attribute>
	      		<xsl:value-of select=" $homeLoses + $awayLoses"/>
	      	</loses>
	      	<goalsScored>
		      	<xsl:attribute name="home">
			    <xsl:value-of select="$homeGoalsScored" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayGoalsScored" />
			  </xsl:attribute>
	      		<xsl:value-of select="$homeGoalsScored + $awayGoalsScored"/>
	      	</goalsScored>
	      	<goalsConceded>
		      	<xsl:attribute name="home">
			    <xsl:value-of select="$homeGoalsConceded" />
			  </xsl:attribute>
			  <xsl:attribute name="away">
			    <xsl:value-of select="$awayGoalsConceded" />
			  </xsl:attribute>
	      		<xsl:value-of select="$homeGoalsConceded + $awayGoalsConceded"/>
	      	</goalsConceded>
	      	<avgGoalsScored><xsl:value-of select="($homeGoalsScored + $awayGoalsScored) div $gamesPlayed"/></avgGoalsScored>
		<avgGoalsConceded><xsl:value-of select="($homeGoalsConceded + $awayGoalsConceded) div $gamesPlayed"/></avgGoalsConceded>
	      </team>
      </xsl:for-each>
      	  </teams>
	</xsl:template>


</xsl:stylesheet>
